package com.xiaoma.common.extend

import android.animation.ObjectAnimator
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat

/**
 * author: liangjingbo
 * date: 2022/6/21
 * describe:
 */

fun View?.hideSoftInput() {
    if (this == null) return
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0) //强制隐藏键盘
}

fun View.animTranslationY(
    start: Float,
    end: Float,
    duration: Long = 400
) {
    ObjectAnimator.ofFloat(this, View.TRANSLATION_Y, start, end).also {
        it.duration = duration
        it.start()
    }
}

fun View.animAlpha(
    start: Float = this.alpha,
    end: Float,
    duration: Long = 400
) {
    ObjectAnimator.ofFloat(this, View.ALPHA, start, end).also {
        it.duration = duration
        it.start()
    }
}


fun View?.hide(gone: Boolean = true) {
    if (this == null) return
    if (gone && this.visibility != View.GONE) {
        visibility = View.GONE
    }
    if (!gone && this.visibility != View.INVISIBLE) {
        visibility = View.INVISIBLE
    }
}

fun View?.show() {
    if (this == null || visibility == View.VISIBLE) return
    visibility = View.VISIBLE
}

fun View.getColorRes(@ColorRes id: Int) = ContextCompat.getColor(context, id)

fun View.getDrawableRes(@DrawableRes id: Int) = ContextCompat.getDrawable(context, id)
